home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / sun_patch.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1998-07-17  |  5KB  |  195 lines

  1. #!/bin/sh
  2. #
  3. # Protect SPARC stack against unwanted exec access
  4. # Side effect: growth in data segment also loses exec bit.
  5. # This may break some programs.
  6. #
  7. # Install as:
  8. #       /etc/init.d/protect_stack
  9. #       ln /etc/init.d/protect_stack /etc/rc2.d/S07protect_stack
  10. #
  11. # And all programs except init are protected after the next reboot.
  12. #
  13. # After installing the scripts, first test with:
  14. #
  15. #       /etc/init.d/protect_stack start
  16. #
  17. #    Then start a new shell and test changes with /usr/proc/bin/pmap.
  18. #
  19. #       csh -fi
  20. #       % pmap $$
  21. #       ......
  22. #       00047000   56K read/write               - instead of rwx
  23. #       0004D000   32K     [ heap ]
  24. #       ......
  25. #       EFFFC000    8K read/write               - instead of rwx
  26. #       EFFFC000   16K     [ stack ]
  27. #       EFFFE000    8K read/write
  28. #
  29. #
  30. # Seems to work on 2.4/2.5/2.5.1 but this may vary by patchlevel.
  31. # Not all Sun MMUs support this, but it seems to haev effect on sun4m and
  32. # sun4u, probably won't have an effect on sun4c.
  33. #
  34. # The assembly checking may need tweaking depending on OS level and
  35. # patchlevel.
  36. #
  37. # Casper Dik (Casper.Dik@Holland.Sun.COM)
  38. #
  39. # The contents of this file  are intended to  be read as
  40. # an example.  This  is not  a  supported product of Sun
  41. # Microsystems  and  no hotline calls  will  be accepted
  42. # which directly relate to this information.
  43. #
  44. # NO LIABILITY WILL BE  ACCEPTED BY SUN MICROSYSTEMS FOR
  45. # ANY LOSS (DIRECT OR CONSEQUENTIAL) INCURRED IN ANY WAY
  46. # BY ANY PARTY THROUGH THE USE OF THIS INFORMATION.
  47. #
  48. # NO WARRANTY  OF  ANY SORT  IS IMPLIED OR GIVEN FOR ANY
  49. # CODE DERIVED FROM THIS INFORMATION.
  50.  
  51. PATH=/usr/bin:$PATH
  52.  
  53. #
  54. #
  55. # Set/get values using adb.
  56. #
  57. getvalue ()
  58. {
  59.     echo $1/$2 | adb -k /dev/ksyms /dev/mem | awk  "\"$1:\""' == $1 {print $2}'
  60. }
  61. setvalue ()
  62. {
  63.     echo $1/$2$3 | adb -wk /dev/ksyms /dev/mem >/dev/null 2>&1
  64. }
  65.  
  66. #
  67. # Check whether setting/unsetting is not dangerous.
  68. #
  69.  
  70. check ()
  71. {
  72.     map=`getvalue $mapaddr X`
  73.     zfod=`getvalue $zfodaddr x`
  74.     if [ "$map" = "$oldmap" -a "$zfod" = "$oldzfod" ]
  75.     then
  76.         old=true;
  77.     else
  78.         old=false
  79.     fi
  80.     if [ "$map" = "$newmap" -a "$zfod" = "$newzfod" ]
  81.     then
  82.         new=true
  83.     else
  84.         new=false
  85.     fi
  86. }
  87.  
  88.  
  89. p=`basename $0`
  90. zfodaddr=zfod_segvn_crargs+0xd
  91. case "`uname -p`" in
  92. sparc)
  93.  
  94.         #
  95.         # Instruction should at $mapaddr should be: mov 0xf,%reg or mov 0xb,%reg
  96.         # this is a synthetic instruction that encodes as or %g0,0xf,$reg
  97.         # 10rr rrr0 0001 0000 0010 0000 0000 1x11
  98.         #
  99.         # Try and find it at several locations.  Addresses must be specified
  100.         # the way adb prints them.
  101.         #
  102.         for mapaddr in map_hunk+8 map_hunk+0xc
  103.         do
  104.             mapval=`getvalue $mapaddr X`
  105.             case $mapval in
  106.             [9ab][02468ace]10200[bf])
  107.                 reg=`expr $mapval : '\(..\)'`
  108.                 break;;
  109.             esac
  110.         done
  111.         if [ -z "$reg" ]
  112.         then
  113.             echo "${p}: Instruction doesn't match" 1>&2
  114.             exit 1
  115.         fi
  116.  
  117.         echo "${p}: Instruction prefix set to $reg ($mapval@$mapaddr)"
  118.  
  119.         oldmap=${reg}10200f
  120.         newmap=${reg}10200b
  121.         oldzfod=f0f
  122.         newzfod=b0f
  123.  
  124. ;;
  125. i386)
  126.         # Try and find it at several locations.  Addresses must be specified
  127.         # the way adb prints them.
  128.         #
  129.         for mapaddr in map_hunk+0x19
  130.         do
  131.             mapval=`getvalue $mapaddr X`
  132.             case $mapval in
  133.             [bf]f545c6)
  134.                 reg=true
  135.                 break;;
  136.             esac
  137.         done
  138.         if [ -z "$reg" ]
  139.         then
  140.             echo "${p}: Instruction doesn't match" 1>&2
  141.             exit
  142.         fi
  143.         oldmap=ff545c6
  144.         newmap=bf545c6
  145.         oldzfod=f0f
  146.         newzfod=f0b
  147.  
  148. ;;
  149. *)
  150.         echo "Unknown kernel arch"
  151.         exit 1
  152. ;;
  153. esac
  154.  
  155. case "$1" in
  156. start)
  157.     check
  158.     if $new
  159.     then
  160.         echo "${p}: Stack already protected" 1>&2
  161.         exit 0
  162.     fi
  163.     if $old
  164.     then
  165.         setvalue $mapaddr W $newmap
  166.         setvalue $zfodaddr w $newzfod
  167.         echo "${p}: Stack protected"
  168.     else
  169.         echo "${p}: Kernel value mismatch $map != $oldmap or $zfod != $oldzfod" 1>&2
  170.         exit 1
  171.     fi
  172.     ;;
  173. stop)
  174.     check
  175.     if $old
  176.     then
  177.         echo "${p}: Stack already unprotected" 1>&2
  178.         exit 0
  179.     fi
  180.     if $new
  181.     then
  182.         setvalue $mapaddr W $oldmap
  183.         setvalue $zfodaddr w $oldzfod
  184.         echo "${p}: Stack no longer protected"
  185.     else
  186.         echo "${p}: Kernel value mismatch $map != $newmap or $zfod != $newzfod" 1>&2
  187.         exit 1
  188.     fi
  189.     ;;
  190. *)
  191.     echo "Usage: ${p} [start|stop]" 1>&2
  192.     exit 1;;
  193. esac
  194.  
  195.